#include "qipan.h"
#include<QPainter>
#include <QDebug>
#include <QMessageBox>

QiPan::QiPan(QWidget *parent)
    : QWidget{parent},
m_hoverRow(-1),
m_hoverCol(-1),
m_hoverFlag(false)


{
    for (int idx = 0;idx < m_boardSize; idx++)
    {
        for(int jdx = 0; jdx < m_boardSize; jdx++)
        {
            m_board[idx][jdx] = EMPTY;
        }
    }          //初始化清空棋盘





    setMouseTracking(true);  //设置鼠标追踪

}


 void QiPan::paintEvent(QPaintEvent *event) //绘画事件
{
     QPainter painter(this);  //创建画家类

    painter.setRenderHint(QPainter::Antialiasing);  //去除毛刺，抗锯齿

     QColor background = QColor(118,104,154);  //淡灰色
    painter.fillRect(rect(),background);

     for(int idx = 0;idx < m_boardSize;idx++)  //绘制棋盘
    {
         painter.drawLine(m_margin+idx*m_cellSize,m_margin,
                          m_margin+idx*m_cellSize,m_margin+(m_boardSize-1)*m_cellSize) ;  //绘制竖线

         painter.drawLine(m_margin, m_margin + idx * m_cellSize,
                          m_margin + (m_boardSize - 1)*m_cellSize,m_margin + idx * m_cellSize); //绘制横线
     }
     /*绘制悬停点坐标*/
     if(m_hoverFlag ==true &&(m_hoverRow >=0&& m_hoverRow <m_boardSize)&&(m_hoverCol >=0 && m_hoverCol <m_boardSize)&&m_board[m_hoverRow][m_hoverCol] ==EMPTY)
     {
         painter.setBrush(Qt::red);  //设置笔刷
         painter.drawEllipse(QPoint(m_margin + m_hoverRow*m_cellSize,m_margin + m_hoverCol * m_cellSize),
                             m_cellSize / 6,m_cellSize/6);
     }

     for(int row=0;row<m_boardSize;row++)     //绘制棋子
     {
         for(int col=0;col<m_boardSize;col++)
         {
             if(m_board[row][col]==PLAYER)//玩家
             {
                 painter.setBrush(Qt::black);
                 painter.drawEllipse(QPoint(m_margin+row*m_cellSize,m_margin+col*m_cellSize),
                                     m_cellSize/3,m_cellSize/3);
             }
             else if(m_board[row][col]==COMPUTER)//人机
             {
                 painter.setBrush(Qt::white);
                 painter.drawEllipse(QPoint(m_margin+row*m_cellSize,m_margin+col*m_cellSize),
                                     m_cellSize/3,m_cellSize/3);
             }
         }
     }




}
void QiPan::mouseMoveEvent(QMouseEvent *event) //鼠标移动事件
{
    int row = (event->x()- m_margin+(m_cellSize /2))/m_cellSize;
    int col = (event->y() - m_margin+(m_cellSize/2))/m_cellSize;


    if((row >=0&&row <m_boardSize)&&(col >=0&&col< m_boardSize))
    {
        if(m_hoverRow !=row|| m_hoverCol !=col)
        {
            m_hoverRow = row;
            m_hoverCol = col;
            m_hoverFlag = true;   //手动触发绘画事件
            update();
        }
    }
   else
    {
        m_hoverFlag = false;

        m_hoverRow = m_hoverCol = -1;  //鼠标不在棋盘，清空悬停点坐标


        update();
    }
}
void QiPan::mousePressEvent(QMouseEvent *event)    //鼠标点击事件
{
    int row = (event->x()- m_margin+(m_cellSize /2))/m_cellSize;
    int col = (event->y() - m_margin+(m_cellSize/2))/m_cellSize;

    /*判断鼠标点击是否在棋盘内部*/
    if((row>=0&&row<m_boardSize)&&(col>=0&&col<m_boardSize)&&m_board[row][col]==EMPTY)
    {
        m_board[row][col] = PLAYER;  //插入数据

        update();
        bool res =isCheckWin(row,col,PLAYER);
        if(res==true)
        {
            QMessageBox::information(this,"游戏结束","玩家获胜");     //判断玩家是否获胜
        }
    }
}
bool QiPan::isCheckWin(int row,int col,ROLE role)
{
    int dierctions[4][2] =
    {
        {1,0},
        {0 ,1},
        {1,-1},
        {1,1},
        };            //四个方向

    for(int direct = 0;direct<4;direct++)   //遍历每一个方向
    {
        int cnt=1;
        for(int idx=1;idx<5;idx++)             //向一个方向探测
        {
            int nextRow=row+dierctions[direct][0]*idx;
            int nextCol=col+dierctions[direct][1]*idx;     //下一个点的坐标

            if((nextRow>=0&&nextRow<m_boardSize)&&(nextCol>=0&&nextCol<m_boardSize)&&m_board[nextRow][nextCol]==role)
            {
                cnt++;
            }
            else
            {
                break;

            }
        }
        for(int idx=1;idx<5;idx++)             //向相反方向探测
        {
            int nextRow=row-dierctions[direct][0]*idx;
            int nextCol=col-dierctions[direct][1]*idx;     //下一个点的坐标

            if((nextRow>=0&&nextRow<m_boardSize)&&(nextCol>=0&&nextCol<m_boardSize)&&m_board[nextRow][nextCol]==role)
            {
                cnt++;
            }
            else
            {
                break;

            }
        }

        if(cnt>=5)
        {
            return true;
        }


    }
}

















